import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../../generated/AlphaResponse.pb.dart';
import '../../../../generated/ConnectRobotWifi.pb.dart';
import '../../../models/WifiInfo.dart';
import '../../../robot_comm/cmd_const.dart';
import '../../../robot_comm/mqtt_comm_manager.dart';
import '../../../widget/tips.dart';

class WifiPassword2Controller extends GetxController {
  var passwordTextEdit = TextEditingController().obs;
  var pwVisible = false.obs;
  var wifiInfoMap = {}.obs;

  var  wifiInfo = WifiInfo().obs;

  @override
  void onInit() {
    super.onInit();
    wifiInfo.value = Get.arguments;
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void configRoboteWifiPw() {
    if (passwordTextEdit.value.text.trim() == '') {
      Tips.showToast('请输入密码.');
      return;
    }

    Tips.showLoading();
    ConnectRobotWifiRequest connectRobotWifiRequest = ConnectRobotWifiRequest();
    connectRobotWifiRequest.ssid = wifiInfo.value.s!;
    connectRobotWifiRequest.cap = wifiInfo.value.c!;
    connectRobotWifiRequest.pwd = passwordTextEdit.value.text;

    MqttCommManager().sendCmd(CmdConst.IM_SEND_WIFI_TO_ROBOT_REQUEST, connectRobotWifiRequest, (AlphaResponse msg) {
      Tips.dismissLoading();
      print('connectRobotWifiResponse result : ${msg}');
      ConnectRobotWifiResponse connectRobotWifiResponse  = ConnectRobotWifiResponse.fromBuffer(msg.bodyData);
      print('connectRobotWifiResponse result : ${connectRobotWifiResponse}');
     /* if(connectRobotWifiResponse.code == '1') {
        Tips.showToast('Wi-Fi密码无效.');
      }*/



    });
  }

}
